<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="680"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '修改账号' : '添加账号'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      ref="formRef"
      :model="form"
      :rules="rules"
      :label-col="{ md: 7, sm: 24 }"
      :wrapper-col="{ md: 17, sm: 24 }"
    >
      <a-row :gutter="16">
        <a-col :md="12" :sm="24" :xs="24">
          <a-form-item label="所属机构">
            <org-select
              placeholder="请选择所属机构"
              v-model:value="form.deviceOrg"
              @update:value="updateDeviceOrg"
            />
          </a-form-item>
          <a-form-item label="对讲机账号" name="deviceNo">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入用户账号"
              v-model:value="form.deviceNo"
            />
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24" :xs="24">
          <a-form-item label="设备型号" name="deviceModel">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入设备型号"
              v-model:value="form.deviceModel"
            />
          </a-form-item>
          <a-form-item label="注册时间" name="registerTime">
            <a-date-picker
              show-time
              class="ele-fluid"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              v-model:value="form.registerTime"
              placeholder="选择日期" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row  :gutter="3">
        <a-col :md="24" :sm="24" :xs="24">
          <a-form-item label="对讲机组" :label-col="{ md: 3, sm: 3, xs: 24 }">
            <group-select
              :data="props.allGroupList"
              placeholder="请选择对讲机组"
              :deviceOrg="form.deviceOrg"
              v-model:value="form.groupList"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item label="备注" name="meta" :label-col="{ md: 3, sm: 4, xs: 24 }" :wrapper-col="{ md: 21, sm: 20, xs: 24 }" >
        <a-textarea :rows="4" :maxlength="200" placeholder="请输入备注信息" v-model:value="form.comments"/>
      </a-form-item>
    </a-form>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { emailReg, phoneReg } from 'ele-admin-pro/es';
  import useFormData from '@/utils/use-form-data';
  import OrgSelect from './org-select.vue';
  import GroupSelect from './group-select.vue';
  import { addDevice, updateDevice } from '@/api/audio/device';

  const emit = defineEmits(['done', 'update:visible']);

  const props = defineProps({
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    // 全部机构
    // organizationList: Array,
    // 所有对讲机组
    allGroupList: Array,
    // 对讲机账号id
    deviceId: Number
  });

  //
  const formRef = ref(null);

  // 是否是修改
  const isUpdate = ref(false);

  // 提交状态
  const loading = ref(false);

  // 表单数据
  const { form, resetFields, assignFields } = useFormData({
    id: undefined,
    // 对讲组
    groupId: undefined,
    deviceNo: '',
    deviceOrg: '',
    deviceModel: '',
    registerTime: '',
    groupList: undefined,
    comment: ''
  });

  // 表单验证规则
  const rules = reactive({
    deviceNo: [
      {
        required: true,
        message: '请输入对讲机账号',
        type: 'string',
        trigger: 'blur'
      }
    ],
    deviceOrg: [
      {
        required: true,
        message: '请选择所属机构',
        type: 'string',
        trigger: 'blur'
      }
    ],
    deviceModel: [
      {
        required: true,
        message: '请输入设备型号',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /* 保存编辑 */
  const save = () => {
    if (!formRef.value) {
      return;
    }
    formRef.value
      .validate()
      .then(() => {
        loading.value = true;
        const saveOrUpdate = isUpdate.value ? updateDevice : addDevice;
        saveOrUpdate(form)
          .then((msg) => {
            loading.value = false;
            message.success(msg);
            updateVisible(false);
            emit('done');
          })
          .catch((e) => {
            loading.value = false;
            message.error(e.message);
          });
      })
      .catch(() => {});
  };

  /* 更新visible */
  const updateVisible = (value) => {
    emit('update:visible', value);
  };

  const updateDeviceOrg = (value) => {
    console.log('updateDeviceOrg=====>',value);
  };

  watch(
    () => props.visible,
    (visible) => {
      if (visible) {
        if (props.data) {
          assignFields(props.data);
          isUpdate.value = true;
        } else {
          form.id = props.deviceId;
          isUpdate.value = false;
        }
      } else {
        resetFields();
        formRef.value?.clearValidate();
      }
    }
  );
</script>
